package com.fr.report.io;

import com.fr.base.FRContext;
import com.fr.base.Style;
import com.fr.report.CellElement;
import com.fr.report.PageSet;
import com.fr.report.PaperSetting;
import com.fr.report.Report;
import com.fr.report.ReportGetter;
import com.fr.report.ReportPage;
import com.fr.report.ResultWorkBook;
import com.fr.report.WorkSheet;
import com.fr.report.core.PaintUtils;
import com.fr.report.core.ReportUtils;
import com.fr.report.core.cal.LayerPageReport;
import com.fr.report.core.cal.LayerReport;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.Format;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CSVExporter extends AbstractAppExporter
{
  private void exportReport(OutputStream paramOutputStream, ReportGetter paramReportGetter)
    throws Exception
  {
    WorkSheet localWorkSheet = new WorkSheet();
    Iterator localIterator = paramReportGetter.cellIterator();
    while (localIterator.hasNext())
      localWorkSheet.addCellElement((CellElement)localIterator.next());
    PrintWriter localPrintWriter = new PrintWriter(new OutputStreamWriter(paramOutputStream));
    String str = null;
    for (int i = 0; i < localWorkSheet.getRowCount(); ++i)
    {
      StringBuffer localStringBuffer = new StringBuffer();
      for (int j = 0; j < localWorkSheet.getColumnCount(); ++j)
      {
        CellElement localCellElement = localWorkSheet.getCellElement(j, i);
        if ((localCellElement == null) || (localCellElement.getColumn() != j) || (localCellElement.getRow() != i))
        {
          str = "";
        }
        else
        {
          Object localObject = localCellElement.getValue();
          if (localObject == null)
            localObject = "";
          Style localStyle = localCellElement.getStyle();
          Format localFormat = localStyle.getFormat();
          str = PaintUtils.valueToText(localObject, localFormat);
        }
        if (j != 0)
          localStringBuffer.append(",");
        localStringBuffer.append(str);
      }
      if (localStringBuffer != null)
        localPrintWriter.println(localStringBuffer.toString());
    }
    localPrintWriter.flush();
  }

  public void export(OutputStream paramOutputStream, ResultWorkBook paramResultWorkBook)
    throws Exception
  {
    List localList = ReportUtils.getPaperSettingListFromWorkBook(paramResultWorkBook);
    int i = 0;
    int j = paramResultWorkBook.getReportCount();
    while (i < j)
    {
      export(paramOutputStream, paramResultWorkBook.getReport(i), (PaperSetting)localList.get(i));
      ++i;
    }
  }

  private void export(OutputStream paramOutputStream, Report paramReport, PaperSetting paramPaperSetting)
    throws Exception
  {
    if ((paramReport instanceof LayerReport) || (paramReport instanceof LayerPageReport))
    {
      FRContext.getLogger().log(Level.INFO, "LayerReport start export");
      PageSet localPageSet = paramReport.generateReportPageSet(paramPaperSetting).getTraversingCase();
      export(paramOutputStream, localPageSet);
      localPageSet.release();
    }
    else if (paramReport != null)
    {
      FRContext.getLogger().log(Level.INFO, "UnLayerReport start export");
      exportReport(paramOutputStream, paramReport);
    }
  }

  public void export(OutputStream paramOutputStream, PageSet paramPageSet)
    throws Exception
  {
    for (int i = 0; i < paramPageSet.size(); ++i)
    {
      ReportPage localReportPage = paramPageSet.getPage(i);
      if (localReportPage == null)
        return;
      exportReport(paramOutputStream, localReportPage);
    }
  }
}